package com.example.sangong.utils;

import com.example.sangong.entity.Card;
import com.example.sangong.myenum.CardEnum;
import com.example.sangong.myenum.ScoreEnum;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class RuleUtils {

    public  static  ScoreEnum calScore(List<Card> cardList) {
        int points;
        int one = getCode(cardList, 0);
        int two = getCode(cardList, 1);
        int three = getCode(cardList, 2);
        Set<Integer> numSet = new HashSet<>();
        numSet.add(one);
        numSet.add(two);
        numSet.add(three);
        points = one + two + three;
        //三张都一样
        if (numSet.size() == 1) {
            if (numSet.contains(CardEnum.BLACK_J_Q_K_E_R.getVal())) {
                return ScoreEnum.THREE_KING;
            } else {
                return ScoreEnum.THREE_HEAD;
            }
        }
        //两张都一样
        if (numSet.size() == 2) {
            if (points > 200) {
                return ScoreEnum.TWO_KING;
            }
            if (numSet.contains(CardEnum.BLACK_J_Q_K_E_R.getVal())) {
                return ScoreEnum.THREE_HEAD;
            } else {
                if (points > 33) {
                    return ScoreEnum.SAN_GONG;
                }
                return calOneByOne(one, two, three);

            }

        }

        //三张都不同
        //包含王的情况
        if (numSet.contains(CardEnum.BLACK_J_Q_K_E_R.getVal())) {
            if (points > 122) {
                return ScoreEnum.SAN_GONG;
            } else {
                //重新计算
                return calOneByOne(one, two, three);
            }
        }
        //不包含王的情况
        if (points > 33) {
            return ScoreEnum.SAN_GONG;
        }

        //重新计算
        return calOneByOne(one, two, three);


    }


    /**
     * 庄家算积分
     *
     * @param master       庄家的成绩
     * @param follower     闲家的成绩
     * @param followerChip 闲家的筹码
     * @return
     */
    public static int calCredit(ScoreEnum master, ScoreEnum follower, int followerChip) {
        //闲家一点一定被吃
        if (follower.getPoints() <= 1) {
            return  followerChip;
        }
        if (master.getPoints() ==  follower.getPoints()) {
            return 0;
        }
        //庄家大
        if (master.getPoints() > follower.getPoints()) {
            return followerChip * master.getMultiple();
        }
        //闲家大
        return -1 * followerChip * follower.getMultiple();

    }

    private static ScoreEnum calOneByOne(int one, int two, int three) {
        //重新计算
        int points = 0;
        if (one < 10) {
            points += one;
        }
        if (two < 10) {
            points += two;
        }
        if (three < 10) {
            points += three;
        }
        return ScoreEnum.getScore(points % 10);
    }

    public static int getCode(List<Card> cardList, int index) {
        return cardList.get(index).getCardEnum().getVal();
    }



}
